package com.zhouchao.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhouchao.user.common.Result;
import com.zhouchao.user.domain.LagouToken;
import com.zhouchao.user.domain.LagouUser;
import com.zhouchao.user.feign.code.CodeServiceFeignClient;
import com.zhouchao.user.mapper.LagouTokenMapper;
import com.zhouchao.user.mapper.LagouUserMapper;
import com.zhouchao.user.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

/**
 * @Author: zhouchao
 * @Date: 2021/10/15 21:34
 * @Description:
 */
@Slf4j
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private CodeServiceFeignClient codeServiceFeignClient;

    @Autowired
    private LagouUserMapper lagouUserMapper;

    @Override
    @Transactional
    public Result<Integer> register(String email, String password, String code) {
        // 校验验证码
        Result<Integer> checkCode = codeServiceFeignClient.validateCode(email, code);
        if (checkCode == null || !checkCode.getCode().equals("0")) {
            return Result.failure("500", checkCode.getMsg(), 2);
        }

        // 验证邮箱是否已经被注册过了
        int count = lagouUserMapper.selectCount(new QueryWrapper<LagouUser>().lambda()
                .eq(LagouUser::getEmail, email));
        if (count > 0) {
            return Result.failure("500", "此邮箱已经被注册过了", 3);
        }

        // 注册用户
        LagouUser lagouUser = new LagouUser();
        lagouUser.setEmail(email);
        lagouUser.setPassword(password);
        int i = lagouUserMapper.insert(lagouUser);
        if (i > 0) {
            return Result.success(0);
        }
        return Result.failure("500", "注册失败", 1);
    }

    @Autowired
    private LagouTokenMapper lagouTokenMapper;

    @Override
    public Result<Boolean> isRegistered(String email) {
        List<LagouUser> lagouUserList = lagouUserMapper.selectList(new QueryWrapper<LagouUser>().lambda()
                .eq(LagouUser::getEmail, email));
        if (CollectionUtils.isEmpty(lagouUserList)) {
            return Result.success(false);
        }
        return Result.success(true);
    }

    @Override
    public Result<String> login(String email, String password, HttpServletRequest request, HttpServletResponse response) {
        LagouUser lagouUser = lagouUserMapper.selectOne(new QueryWrapper<LagouUser>().lambda()
                .eq(LagouUser::getEmail, email)
                .eq(LagouUser::getPassword, password));
        if (lagouUser == null) {
            return Result.failure("500", "用户名密码错误", "false");
        }

        String token = UUID.randomUUID().toString();
        LagouToken lagouToken = new LagouToken();
        lagouToken.setEmail(email);
        lagouToken.setToken(token);
        int i = lagouTokenMapper.insert(lagouToken);
        if (i > 0) {
            Cookie cookie = new Cookie("token", token);
            cookie.setPath("/");
            cookie.setDomain("www.test.com");
            response.addCookie(cookie);
        }

        return Result.success(lagouUser.getEmail());
    }

    @Override
    public Result<String> getInfo(String token) {
        LagouToken lagouToken = lagouTokenMapper.selectOne(new QueryWrapper<LagouToken>().lambda()
                .eq(LagouToken::getToken, token)
                .orderByDesc());
        if (lagouToken == null) {
            return Result.failure("500", "未查到相关用户", null);
        }

        return Result.success(lagouToken.getEmail());
    }
}
